<!-- eslint-disable vue/no-useless-template-attributes -->
<template>
	<a-card :bordered="false">
		<a-form ref="searchFormRef" name="advanced_search" :model="searchFormState" class="ant-advanced-search-form">
			<a-row :gutter="24">
				<a-col :span="6">
					<a-form-item label="申请人姓名" name="userName">
						<a-input v-model:value="searchFormState.userName" placeholder="请输入申请人姓名" />
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="工作单位" name="workOrg">
						<a-input v-model:value="searchFormState.workOrg" placeholder="请输入工作单位" />
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-button type="primary" @click="tableRef.refresh()">查询</a-button>
					<a-button style="margin: 0 8px" @click="reset">重置</a-button>
				</a-col>
			</a-row>
		</a-form>
		<s-table
			ref="tableRef"
			:columns="columns"
			:data="loadData"
			:alert="options.alert.show"
			bordered
			:row-key="(record) => record.id"
			:tool-config="toolConfig"
		>
			<!-- <template #operator class="table-operator">
                <a-space>
                    <a-button type="primary" @click="formRef.onOpen()" v-if="hasPerm('bizHelpAdd')">
                        <template #icon><plus-outlined /></template>
                        新增
                    </a-button>
                    <xn-batch-delete v-if="hasPerm('bizHelpBatchDelete')" :selectedRowKeys="selectedRowKeys"
                        @batchDelete="deleteBatchBizHelp" />
                </a-space>
            </template> -->
			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'health'">
					{{ $TOOL.dictTypeData('健康情况', record.health) }}
				</template>
				<template v-if="column.dataIndex === 'houseType'">
					{{ $TOOL.dictTypeData('房屋类型', record.houseType) }}
				</template>
				<template v-if="column.dataIndex === 'fileUrl'">
					<a :href="record.fileUrl">文件下载</a>
				</template>
				<template v-if="column.dataIndex === 'action'">
					<template v-if="column.dataIndex === 'action'">
						<div v-if="record.status < 2">
							<a-space>
								<!-- <a-popconfirm title="确认通过？"> -->
								<a v-if="hasPerm('bizNewContentEdit')" @click="formRef.onOpen(record, 1)">审核</a>
								<!-- </a-popconfirm> -->

								<!-- <a-divider type="vertical" v-if="hasPerm(['bizHelpEdit', 'bizHelpDelete'], 'and')" />
                                <a-button type="link" danger size="small" v-if="hasPerm('bizHelpEdit')"
                                    @click="formRef.onOpen(record, columnOptions)">退回</a-button> -->
							</a-space>
						</div>
						<div v-else>
							<a-space>
								<a-button type="link" size="small" @click="viewRef.onOpen(record, 2)">查看</a-button>
							</a-space>
						</div>
					</template>
					<!-- <a-space>
						<a @click="formRef.onOpen(record)" v-if="hasPerm('bizHelpEdit')">编辑</a>
						<a-divider type="vertical" v-if="hasPerm(['bizHelpEdit', 'bizHelpDelete'], 'and')" />
						<a-popconfirm title="确定要删除吗？" @confirm="deleteBizHelp(record)">
							<a-button type="link" danger size="small" v-if="hasPerm('bizHelpDelete')">删除</a-button>
						</a-popconfirm>
					</a-space> -->
				</template>
			</template>
		</s-table>
	</a-card>
	<Form ref="formRef" @successful="tableRef.refresh()" />
	<View ref="viewRef"></View>
</template>

<script setup name="help">
	import View from './view.vue'
	import { cloneDeep } from 'lodash-es'
	import Form from './form.vue'
	import bizHelpApi from '@/api/biz/bizHelpApi'
	const searchFormState = ref({})
	const searchFormRef = ref()
	const tableRef = ref()
	const formRef = ref()
	const viewRef = ref()
	const toolConfig = { refresh: true, height: true, columnSetting: true, striped: false }
	const columns = [
		// {
		// 	title: '申请人',
		// 	dataIndex: 'userId'
		// },
		{
			title: '申请人姓名',
			dataIndex: 'userName'
		},
		{
			title: '生日',
			dataIndex: 'birthday'
		},
		{
			title: '健康情况',
			dataIndex: 'health'
		},
		{
			title: '身份证号',
			dataIndex: 'idCard'
		},
		{
			title: '工作年限',
			dataIndex: 'workRange'
		},
		{
			title: '工作单位',
			dataIndex: 'workOrg'
		},
		{
			title: '家庭住址',
			dataIndex: 'address'
		},
		{
			title: '房屋类型',
			dataIndex: 'houseType'
		},
		{
			title: '房屋面积',
			dataIndex: 'houseArea'
		},
		{
			title: '联系电话',
			dataIndex: 'phone'
		},
		{
			title: '工作职务',
			dataIndex: 'duites'
		},
		{
			title: '自付金额',
			dataIndex: 'selfPay'
		},
		{
			title: '家庭人口',
			dataIndex: 'familyNum'
		},
		{
			title: '本人平均月收入（元/人）',
			dataIndex: 'averageIncome'
		},
		{
			title: '家庭总收入（元）',
			dataIndex: 'totalIncome'
		},
		{
			title: '家庭平均收入',
			dataIndex: 'familyAverageIncome'
		},
		{
			title: '附件查看',
			dataIndex: 'fileUrl'
		}
		// {
		// 	title: '致困原因',
		// 	dataIndex: 'helpConten'
		// }
	]
	// 操作栏通过权限判断是否显示
	if (hasPerm(['bizHelpEdit', 'bizHelpDelete'])) {
		columns.push({
			title: '操作',
			dataIndex: 'action',
			align: 'center',
			width: 150
		})
	}
	const selectedRowKeys = ref([])
	// 列表选择配置
	const options = {
		// columns数字类型字段加入 needTotal: true 可以勾选自动算账
		alert: {
			show: true,
			clear: () => {
				selectedRowKeys.value = ref([])
			}
		},
		rowSelection: {
			onChange: (selectedRowKey, selectedRows) => {
				selectedRowKeys.value = selectedRowKey
			}
		}
	}
	const loadData = (parameter) => {
		const searchFormParam = cloneDeep(searchFormState.value)
		return bizHelpApi.bizHelpApplyPage(Object.assign(parameter, searchFormParam)).then((data) => {
			return data
		})
	}
	// 重置
	const reset = () => {
		searchFormRef.value.resetFields()
		tableRef.value.refresh(true)
	}
	// 删除
	const deleteBizHelp = (record) => {
		let params = [
			{
				id: record.id
			}
		]
		bizHelpApi.bizHelpDelete(params).then(() => {
			tableRef.value.refresh(true)
		})
	}
	// 批量删除
	const deleteBatchBizHelp = (params) => {
		bizHelpApi.bizHelpDelete(params).then(() => {
			tableRef.value.clearRefreshSelected()
		})
	}

	const applyBizHelp = (params) => {
		let data = Object.assign({}, params)
		data.status = 1
		bizHelpApi.applyBizHelp(data).then(() => {
			tableRef.value.clearRefreshSelected()
		})
	}
</script>
